package crm.utility;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class MyTableModel extends DefaultTableModel {
	private static final long serialVersionUID = 1L;
	
	private String[] COLUMN_NAMES = new String[] {"Name", "Stuff", "Button1", "Button2"};
	private Class<?>[] COLUMN_TYPES = new Class<?>[] {Integer.class, String.class, JButton.class,  JButton.class};
	
	MyTableModel()
	{
		super();
	}
	
	MyTableModel(String[]clumnNames,Class<?>[] className)
	{
		this.COLUMN_NAMES=clumnNames;
		this.COLUMN_TYPES=className;
	}
	
	
	@Override public int getColumnCount() {
		return COLUMN_NAMES.length;
	}

	@Override public int getRowCount() {
		return this.dataVector.size();
	}
	
	@Override public String getColumnName(int columnIndex) {
        return COLUMN_NAMES[columnIndex];
    }
	
	@Override public Class<?> getColumnClass(int columnIndex) {
		return COLUMN_TYPES[columnIndex];
	}

	@Override public Object getValueAt(final int rowIndex, final int columnIndex) {
		switch (columnIndex) {
			case 0: return rowIndex;
			case 1: return "Text for "+rowIndex;
			case 2: // fall through
			case 3: final JButton button = new JButton(COLUMN_NAMES[columnIndex]);
					button.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent arg0) {
							JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(button), 
									"Button clicked for row "+rowIndex);
						}
					});
					return button;
			default: return "Error";
		}
	}	
}
